"""
配置预设 API Schema
"""
from pydantic import BaseModel, Field, field_validator
from typing import Optional, List
from datetime import datetime


# ========== 应用配置 ==========
class ApplicationCreate(BaseModel):
    """创建应用请求"""
    name: str = Field(..., description="应用名称", min_length=1, max_length=255)
    description: Optional[str] = Field(None, description="描述")
    appid: Optional[str] = Field(None, description="应用ID", max_length=255)
    hw_id: Optional[str] = Field(None, description="凭证ID", max_length=255)
    hw_appkey: Optional[str] = Field(None, description="认证密钥", max_length=255)
    vender: Optional[str] = Field(None, description="供应商", max_length=100)
    region: Optional[str] = Field(None, description="区域", max_length=100)
    trainApiVersion: Optional[str] = Field(None, description="训练作业API版本", max_length=100)
    created_by: Optional[str] = Field(None, description="创建人")

    @field_validator('name')
    @classmethod
    def name_must_not_be_empty(cls, v: str) -> str:
        if not v.strip():
            raise ValueError('应用名称不能为空')
        return v.strip()


class ApplicationUpdate(BaseModel):
    """更新应用请求"""
    name: Optional[str] = Field(None, description="应用名称", min_length=1, max_length=255)
    description: Optional[str] = Field(None, description="描述")
    appid: Optional[str] = Field(None, description="应用ID", max_length=255)
    hw_id: Optional[str] = Field(None, description="凭证ID", max_length=255)
    hw_appkey: Optional[str] = Field(None, description="认证密钥", max_length=255)
    vender: Optional[str] = Field(None, description="供应商", max_length=100)
    region: Optional[str] = Field(None, description="区域", max_length=100)
    trainApiVersion: Optional[str] = Field(None, description="训练作业API版本", max_length=100)
    is_active: Optional[bool] = Field(None, description="是否启用")


class ApplicationResponse(BaseModel):
    """应用响应"""
    id: int
    name: str
    description: Optional[str] = None
    appid: Optional[str] = None
    hw_id: Optional[str] = None
    hw_appkey: Optional[str] = None
    vender: Optional[str] = None
    region: Optional[str] = None
    trainApiVersion: Optional[str] = None
    is_active: bool
    created_at: datetime
    updated_at: datetime
    created_by: Optional[str] = None

    model_config = {"from_attributes": True}


class ApplicationListResponse(BaseModel):
    """应用列表响应"""
    total: int
    items: List[ApplicationResponse]


# ========== OBS桶配置预设 ==========
class ObsBucketPresetCreate(BaseModel):
    """创建OBS桶配置预设请求"""
    name: str = Field(..., description="预设名称（中文名称）", min_length=1, max_length=255)
    bucket_id: str = Field(..., description="桶ID", min_length=1, max_length=255)
    bucket_path: Optional[str] = Field(None, description="桶路径", max_length=500)
    description: Optional[str] = Field(None, description="描述")
    application_id: int = Field(..., description="所属应用ID")
    created_by: Optional[str] = Field(None, description="创建人")

    @field_validator('name')
    @classmethod
    def name_must_not_be_empty(cls, v: str) -> str:
        if not v.strip():
            raise ValueError('预设名称不能为空')
        return v.strip()


class ObsBucketPresetUpdate(BaseModel):
    """更新OBS桶配置预设请求"""
    name: Optional[str] = Field(None, description="预设名称", min_length=1, max_length=255)
    bucket_id: Optional[str] = Field(None, description="桶ID", min_length=1, max_length=255)
    bucket_path: Optional[str] = Field(None, description="桶路径", max_length=500)
    description: Optional[str] = Field(None, description="描述")
    application_id: Optional[int] = Field(None, description="所属应用ID")
    is_active: Optional[bool] = Field(None, description="是否启用")


class ObsBucketPresetResponse(BaseModel):
    """OBS桶配置预设响应"""
    id: int
    name: str
    bucket_id: str
    bucket_path: Optional[str] = None
    description: Optional[str] = None
    is_active: bool
    application_id: int
    application: Optional[ApplicationResponse] = None
    created_at: datetime
    updated_at: datetime
    created_by: Optional[str] = None

    model_config = {"from_attributes": True}


class ObsBucketPresetListResponse(BaseModel):
    """OBS桶配置预设列表响应"""
    total: int
    items: List[ObsBucketPresetResponse]


# ========== 资源池配置预设 ==========
class ResourcePoolPresetCreate(BaseModel):
    """创建资源池配置预设请求"""
    name: str = Field(..., description="预设名称（中文名称）", min_length=1, max_length=255)
    pool_type: str = Field(..., description="资源池类型", min_length=1, max_length=100)
    pool_uid: str = Field(..., description="资源池UID", min_length=1, max_length=255)
    spec_name: str = Field(..., description="资源池规格名称", min_length=1, max_length=255)
    flavor_code: str = Field(..., description="资源池编码", min_length=1, max_length=255)
    description: Optional[str] = Field(None, description="描述")
    application_id: int = Field(..., description="所属应用ID")
    created_by: Optional[str] = Field(None, description="创建人")

    @field_validator('name')
    @classmethod
    def name_must_not_be_empty(cls, v: str) -> str:
        if not v.strip():
            raise ValueError('预设名称不能为空')
        return v.strip()


class ResourcePoolPresetUpdate(BaseModel):
    """更新资源池配置预设请求"""
    name: Optional[str] = Field(None, description="预设名称", min_length=1, max_length=255)
    pool_type: Optional[str] = Field(None, description="资源池类型", min_length=1, max_length=100)
    pool_uid: Optional[str] = Field(None, description="资源池UID", min_length=1, max_length=255)
    spec_name: Optional[str] = Field(None, description="资源池规格名称", min_length=1, max_length=255)
    flavor_code: Optional[str] = Field(None, description="资源池编码", min_length=1, max_length=255)
    description: Optional[str] = Field(None, description="描述")
    application_id: Optional[int] = Field(None, description="所属应用ID")
    is_active: Optional[bool] = Field(None, description="是否启用")


class ResourcePoolPresetResponse(BaseModel):
    """资源池配置预设响应"""
    id: int
    name: str
    pool_type: str
    pool_uid: str
    spec_name: str
    flavor_code: str
    description: Optional[str] = None
    is_active: bool
    application_id: int
    application: Optional[ApplicationResponse] = None
    created_at: datetime
    updated_at: datetime
    created_by: Optional[str] = None

    model_config = {"from_attributes": True}


class ResourcePoolPresetListResponse(BaseModel):
    """资源池配置预设列表响应"""
    total: int
    items: List[ResourcePoolPresetResponse]


# ========== 镜像配置预设 ==========
class ImagePresetCreate(BaseModel):
    """创建镜像配置预设请求"""
    name: str = Field(..., description="预设名称（中文名称）", min_length=1, max_length=255)
    image_version: Optional[str] = Field(None, description="镜像版本", max_length=255)
    image_uid: str = Field(..., description="镜像UID", min_length=1, max_length=255)
    image_version_uid: str = Field(..., description="镜像版本UID", min_length=1, max_length=255)
    description: Optional[str] = Field(None, description="描述")
    application_id: int = Field(..., description="所属应用ID")
    created_by: Optional[str] = Field(None, description="创建人")

    @field_validator('name')
    @classmethod
    def name_must_not_be_empty(cls, v: str) -> str:
        if not v.strip():
            raise ValueError('预设名称不能为空')
        return v.strip()


class ImagePresetUpdate(BaseModel):
    """更新镜像配置预设请求"""
    name: Optional[str] = Field(None, description="预设名称", min_length=1, max_length=255)
    image_version: Optional[str] = Field(None, description="镜像版本", max_length=255)
    image_uid: Optional[str] = Field(None, description="镜像UID", min_length=1, max_length=255)
    image_version_uid: Optional[str] = Field(None, description="镜像版本UID", min_length=1, max_length=255)
    description: Optional[str] = Field(None, description="描述")
    application_id: Optional[int] = Field(None, description="所属应用ID")
    is_active: Optional[bool] = Field(None, description="是否启用")


class ImagePresetResponse(BaseModel):
    """镜像配置预设响应"""
    id: int
    name: str
    image_version: Optional[str] = None
    image_uid: str
    image_version_uid: str
    description: Optional[str] = None
    is_active: bool
    application_id: int
    application: Optional[ApplicationResponse] = None
    created_at: datetime
    updated_at: datetime
    created_by: Optional[str] = None

    model_config = {"from_attributes": True}


class ImagePresetListResponse(BaseModel):
    """镜像配置预设列表响应"""
    total: int
    items: List[ImagePresetResponse]


# ========== NAS配置预设 ==========
class NasPresetCreate(BaseModel):
    """创建NAS配置预设请求"""
    name: str = Field(..., description="预设名称（中文名称）", min_length=1, max_length=255)
    nas_name: str = Field(..., description="NAS名称", min_length=1, max_length=255)
    nas_type: str = Field(..., description="NAS类型，如efs", min_length=1, max_length=100)
    nas_uid: str = Field(..., description="NAS UID", min_length=1, max_length=255)
    nas_instance_id: str = Field(..., description="NAS实例ID", min_length=1, max_length=255)
    nas_shar_addr: str = Field(..., description="NAS路径", min_length=1, max_length=500)
    description: Optional[str] = Field(None, description="描述")
    application_id: int = Field(..., description="所属应用ID")
    created_by: Optional[str] = Field(None, description="创建人")

    @field_validator('name')
    @classmethod
    def name_must_not_be_empty(cls, v: str) -> str:
        if not v.strip():
            raise ValueError('预设名称不能为空')
        return v.strip()


class NasPresetUpdate(BaseModel):
    """更新NAS配置预设请求"""
    name: Optional[str] = Field(None, description="预设名称", min_length=1, max_length=255)
    nas_name: Optional[str] = Field(None, description="NAS名称", min_length=1, max_length=255)
    nas_type: Optional[str] = Field(None, description="NAS类型，如efs", min_length=1, max_length=100)
    nas_uid: Optional[str] = Field(None, description="NAS UID", min_length=1, max_length=255)
    nas_instance_id: Optional[str] = Field(None, description="NAS实例ID", min_length=1, max_length=255)
    nas_shar_addr: Optional[str] = Field(None, description="NAS路径", min_length=1, max_length=500)
    description: Optional[str] = Field(None, description="描述")
    application_id: Optional[int] = Field(None, description="所属应用ID")
    is_active: Optional[bool] = Field(None, description="是否启用")


class NasPresetResponse(BaseModel):
    """NAS配置预设响应"""
    id: int
    name: str
    nas_name: str
    nas_type: str
    nas_uid: str
    nas_instance_id: str
    nas_shar_addr: str
    description: Optional[str] = None
    is_active: bool
    application_id: int
    application: Optional[ApplicationResponse] = None
    created_at: datetime
    updated_at: datetime
    created_by: Optional[str] = None

    model_config = {"from_attributes": True}


class NasPresetListResponse(BaseModel):
    """NAS配置预设列表响应"""
    total: int
    items: List[NasPresetResponse]
